import { getBackupList, restoreBackupById } from '@/api/server/backup'

export const useBackup = () => {
  const backupDataList = ref([])
  const total = ref(0)  
  const page = ref(1)
  const pageSize = ref(10)
  const backupType = ref('')
  const dataYear = ref('')

  const getBackupDataList = async () => {
    const res = await getBackupList({ 
      dataYear: dataYear.value, 
      backupType: backupType.value, 
      page: page.value, 
      pageSize: pageSize.value
     })
    backupDataList.value = res.data.data.list
    total.value = res.data.data.total
  }


  const handleReset = () => {
    backupType.value = 'all'
    dataYear.value = ''
    getBackupDataList()
  }
  
  const handleQuery = async () => {
    await getBackupDataList()
    if(backupDataList.value.length > 0) { 
      ElMessage.success('查询成功') 
    } else {
      ElMessage.warning('暂无数据')
    }
  }

  // 还原
  const handleRestore = async (row) => {
    const res = await restoreBackupById({
      backupId: row.id
    })
    if(res.data.code === 200) {
      ElMessage.success('还原成功')
    } else {
      ElMessage.error('还原失败')
    }
  }


  return {
    backupDataList,
    total,
    page,
    pageSize,
    dataYear,
    backupType,
    getBackupDataList,
    handleReset,
    handleQuery,

    handleRestore
  } 
}
